package binarySearchTree;

import student.TestCase;

/**
 * Test BinarySearchTree class.
 * @author xinruili
 * @version 1.0
 */
public class Lab14BinarySearchTreeTest extends TestCase {
    private Lab14BinarySearchTree<String> binarySearchTree;

    /**
     * sets up any needed variables for test methods.
     * @throws Exception sets up variables
     * @author xinruili
     * @version 1.0
     */
    @Override
    public void setUp() throws Exception {
        binarySearchTree = new Lab14BinarySearchTree<>();
    }

    /**
     * test judge the tree is empty.
     */
    public void testIsEmpty() {
        assertEquals(true, binarySearchTree.isEmpty());
        binarySearchTree.insert("element");
        assertEquals(false, binarySearchTree.isEmpty());
    }

    /**
     * test Insert Method.
     */
    public void testInsert() {
        Lab14BinarySearchTree<String> lab14BinarySearchTree
                = new Lab14BinarySearchTree<>();
        lab14BinarySearchTree.insert("123");
        lab14BinarySearchTree.insert("012");
        lab14BinarySearchTree.insert("234");
        try {
            lab14BinarySearchTree.insert("123");
        }
        catch (DuplicateItemException e) {
            assertEquals("123", e.getMessage());
        }
        assertEquals(false, lab14BinarySearchTree.isEmpty());
    }

    /**
     *test Remove.
     */
    public void testRemove() {
        Lab14BinarySearchTree<String> lab14BinarySearchTree
                = new Lab14BinarySearchTree<>();
        try {
            lab14BinarySearchTree.remove("1");
        }
        catch (ItemNotFoundException e) {
            assertEquals("1", e.getMessage());
        }
        lab14BinarySearchTree.insert("123");
        lab14BinarySearchTree.insert("012");
        lab14BinarySearchTree.insert("234");
        //remove right
        lab14BinarySearchTree.remove("234");
        lab14BinarySearchTree.insert("234");
        //remove lift
        lab14BinarySearchTree.remove("012");
        lab14BinarySearchTree.insert("012");
        //remove root
        lab14BinarySearchTree.insert("001");
        lab14BinarySearchTree.insert("345");
        lab14BinarySearchTree.insert("134");
        lab14BinarySearchTree.insert("023");

        lab14BinarySearchTree.remove("123");
        lab14BinarySearchTree.remove("234");
        lab14BinarySearchTree.remove("012");
        assertFalse(lab14BinarySearchTree.isEmpty());
    }

    /**
     *test find.
     */
    public void testFind() {
        Lab14BinarySearchTree<String> lab14BinarySearchTree
                = new Lab14BinarySearchTree<>();
        assertNull(lab14BinarySearchTree.find("123"));
        lab14BinarySearchTree.insert("123");
        lab14BinarySearchTree.insert("456");
        lab14BinarySearchTree.insert("012");
        assertNotNull(lab14BinarySearchTree.find("123"));
        assertNotNull(lab14BinarySearchTree.find("456"));
        assertNotNull(lab14BinarySearchTree.find("012"));
    }

    /**
     * test findMax.
     */
    public void testFindMax() {
        Lab14BinarySearchTree<String> lab14BinarySearchTree
                = new Lab14BinarySearchTree<>();
        assertNull(lab14BinarySearchTree.findMax());
        lab14BinarySearchTree.insert("123");
        assertEquals("123", lab14BinarySearchTree.findMax());
        lab14BinarySearchTree.insert("456");
        lab14BinarySearchTree.insert("789");
        assertEquals("789", lab14BinarySearchTree.findMax());
    }

    /**
     * test findMin.
     */
    public void testFindMin() {
        Lab14BinarySearchTree<String> lab14BinarySearchTree
                = new Lab14BinarySearchTree<>();
        assertNull(lab14BinarySearchTree.findMin());
        lab14BinarySearchTree.insert("123");
        assertEquals("123", lab14BinarySearchTree.findMin());
        lab14BinarySearchTree.insert("456");
        lab14BinarySearchTree.insert("012");
        assertEquals("012", lab14BinarySearchTree.findMin());
    }

    /**
     * test makeEmpty.
     */
    public void testMakeEmpty() {
        binarySearchTree.makeEmpty();
        assertTrue(binarySearchTree.isEmpty());
    }

    /**
     * test getElement.
     */
    public void testGetElements() {
        binarySearchTree.insert("123");
        assertEquals("123",
                binarySearchTree.getElement());
        binarySearchTree.makeEmpty();
        assertNull(binarySearchTree.getElement());
    }

    /**
     * test toString.
     */
    public void testToString() {
        binarySearchTree.makeEmpty();
        assertEquals("()",
                binarySearchTree.toString());
        binarySearchTree.insert("123");
        binarySearchTree.insert("234");
        binarySearchTree.insert("012");
        assertEquals("(012, 123, 234)",
                binarySearchTree.toString());
    }
}
